﻿using Architecture.Domain.Service;
using Architeture.Domain;
using Architeture.Domain.DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ZhuBoLin.Dtos;
using System.Security.Claims;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace RBAC.ZhuBoLin.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AFLController : ControllerBase
    {
        protected readonly IAFLservice aFLservice;

        public AFLController(IAFLservice aFLservice)
        {
            this.aFLservice = aFLservice;
        }
        /// <summary>
        /// 请假添加
        /// </summary>
        /// <param name="a"></param>
        /// <returns>王晓彤细狗</returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AFLadd(Askforleave a)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await aFLservice.AFLadd(a);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返回请假列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<AskPageDto>> GetASKInfo(int pageindex, int pagesize, string? edate, string? odate, string? uname, int? state)
        {
            ApiResult<AskPageDto> result = new ApiResult<AskPageDto>();
            try
            {
                var ids = Convert.ToInt32(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
                result.Data = await aFLservice.GetASKInfo(ids, pageindex, pagesize, edate, odate, uname, state);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 审批反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Askforleave>> SetASKAll(int id)
        {
            ApiResult<Askforleave> result = new ApiResult<Askforleave>();
            try
            {

                result.Data = await aFLservice.SetASKAll(id);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 审批修改
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        /// 
        [HttpPut]
        public async Task<ApiResult<int>> ASKupd(Askforleave a)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                result.Data = await aFLservice.ASKupd(a);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 请假删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> ASKDel(int id)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await aFLservice.ASKDel(id);
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
